const fs = require('fs');
const path = require('path');

const folderPath = 'D:\\桌面\\代码整理\\执业代码整理隐藏注释';
function readFilesRecursively(folderPath) {
  const files = fs.readdirSync(folderPath);
  files.forEach(file => {
    const filePath = path.join(folderPath, file);
    const stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      readFilesRecursively(filePath);
    } else if (stats.isFile()) {
      const list = filePath.split('.')
      const suffix = list[list.length - 1]
      if (suffix === 'vue') {
        setFile(filePath)
      } else if (suffix === 'js') {
        setFile(filePath)
      }
    }
  });
}

function getText (initContent) {
  initContent = initContent.replace(/<!--([\s\S]*?)-->/g, (_,v) => {
    return ''
  })
  initContent = initContent.replace(/\/\*([\s\S]*?)\*\//g, (_,v) => {
    return ''
  })
  let text = ''
  const lines = initContent.split(/\r?\n/);
  lines.forEach((line, index) => {
    text += `
    ${line.replace(/\s*\/\/.*$/, '')}`
  });
  return text
}


function setFile (path) {
  fs.readFile(path, 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return;
    }
    setWriteFile(path,  getText(data))
  });
}

function setWriteFile (path, data) {
  fs.writeFile(path, data, 'utf8', (err) => {
    if (err) {
      console.error(err);
      return;
    }
    // console.log('执行成功')
  });
}
// const folderPath = 'D:\\gitee项目\\微前端调度台\\新建文件夹\\command-main\\src';
readFilesRecursively(folderPath);
